package com.ruoyi.school.file.controller;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.file.InvalidExtensionException;
import com.ruoyi.common.utils.file.FileUploadUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresGuest;
import org.apache.tomcat.jni.FileInfo;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Controller
public class UploadController
{

    @PostMapping("/upload")
    @ResponseBody
    @RequiresAuthentication
    public Map<String,Object> upload(@RequestParam("editormd-image-file") MultipartFile file) throws IOException, InvalidExtensionException {
        // 上传文件路径
        String filePath = RuoYiConfig.getUploadPath();
        // 上传并返回新文件名称
//        String[] f = {"png"};
        String fileName = FileUploadUtils.upload(filePath, file);
        System.out.println(fileName);


        String url = "http://localhost"+fileName.substring(8);
        Map<String,Object> m = new HashMap<>();

        m.put("success",1);
        m.put("url",fileName);
        m.put("message","上传成功");
        return m;
    }
}
